﻿using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace NewBee.Core.Data.Conver
{
    public class ToJson
    {
        /// <summary>
        /// 对象转Json
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ObjectToJson(object obj)
        {
            if (obj == null)
                obj = GetNull();
            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
            timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            string str = Newtonsoft.Json.JsonConvert.SerializeObject(obj, timeFormat);
            if (str == "[]")
                return str = "null";
            return str;
        }

        /// <summary>
        /// 空Json
        /// </summary>
        /// <returns></returns>
        public static string GetNull()
        {
            return "null";
        }
    }
}
